//
//  Main.swift
//  VenusTodo
//
//  Created by 文显庆 on 2021/10/7.
//

import SwiftUI

struct Main: View {
    var body: some View {
        HStack {
            TabView {
                TodoView().tabItem { Image(systemName: "pencil.and.outline")
                    Text("清单")
                }.tag(1)
                TaskCalendar().tabItem { Image(systemName: "calendar.circle")
                    Text("日历")
                }.tag(2)
            }
        }
    }
    init() {
        print("加载完毕")
        
        print("主页面构造器")
    }
}

struct Main_Previews: PreviewProvider {
    static var previews: some View {
        Main()
    }
}

// 清单界面
struct TodoView: View {
    @State var isPressed_addView = false
    @State var isPressed_editView = false
    @State var itemNow = thingDefault
    @State var Tasks = Things
    @State private var showNotDoneOnly = false
    var num: Int {
        get {
            return Tasks.count
        }
    }
    var body: some View {
        VStack {
            NavigationView {
                List {
                    Toggle(isOn: $showNotDoneOnly) {
                        HStack(alignment: .center) {
                            Spacer()
                            Text("Show not done only")
                                .foregroundColor(.blue)
                                .fontWeight(.heavy)
                        }
                        
                            
                    }
                    ForEach(Tasks) { Task in
                        if self.showNotDoneOnly {
                            if !Task.isDone {
                                NavigationLink(destination: TaskItemDetailView(preView: self, task: Task, isdone: Task.isDone, target: String(Task.target), progress: String(Task.progress), taskName: Task.name )){
                                    TaskCell(Task: Task)
                                }
                            }
                        } else {
                            NavigationLink(destination: TaskItemDetailView(preView: self, task: Task, isdone: Task.isDone, target: String(Task.target), progress: String(Task.progress), taskName: Task.name  )){
                                TaskCell(Task: Task)
                            }
                        }
                    }.onDelete(perform: delete)
                    .onMove(perform: move)
        
                    
                }
                .navigationTitle(Text("任务列表"))
                .navigationBarItems(trailing: EditButton())
            }
            
            Button(action: {self.isPressed_addView = true}) {
                                HStack{
                                    Image(systemName: "plus")
                                    Text("Add new a new task")
                                        .padding(.horizontal)
                                }.padding()

                            }
                            .foregroundColor(Color.white)
                            .background(Color.blue)
                            .cornerRadius(8)
                            .padding()
                            .sheet(isPresented: $isPressed_addView, content: {AddThingView(preView: self)})
        }
    }
    
    func delete (at offsets: IndexSet) {
        if let first = offsets.first {
            Tasks.remove(at: first)
        }
        self.save()
    }
    func move (from source: IndexSet, to destination: Int) {
        Tasks.move(fromOffsets: source, toOffset: destination)
}
    func addItem() {
        Tasks.append(thingDefault)
    }
    //本地存储数据
    func save() {
        do{
            let data = try JSONEncoder().encode(self.Tasks)
            UserDefaults.standard.setValue(data, forKey: "Tasks")
        }catch {
            print("Error")
        }
    }
    
}

